package com.example.controller;

import com.example.common.ApiResponse;
import com.example.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private MessageSource messageSource;

    /**
     * 创建用户 - 测试validation
     */
    @PostMapping
    public ApiResponse<User> createUser(@Valid @RequestBody User user) {
        String message = messageSource.getMessage("common.success", null, LocaleContextHolder.getLocale());
        return ApiResponse.success(message, user);
    }

    /**
     * 获取欢迎信息 - 测试i18n
     */
    @GetMapping("/welcome")
    public ApiResponse<String> getWelcomeMessage() {
        String message = messageSource.getMessage("system.welcome", null, LocaleContextHolder.getLocale());
        return ApiResponse.success(message);
    }

    /**
     * 获取问候信息 - 测试i18n参数
     */
    @GetMapping("/hello/{name}")
    public ApiResponse<String> getHelloMessage(@PathVariable String name) {
        String message = messageSource.getMessage("system.hello", new Object[]{name}, LocaleContextHolder.getLocale());
        return ApiResponse.success(message);
    }

    /**
     * 获取当前时间 - 测试i18n参数
     */
    @GetMapping("/time")
    public ApiResponse<String> getCurrentTime() {
        LocalDateTime now = LocalDateTime.now();
        String formattedTime = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String message = messageSource.getMessage("system.current.time", new Object[]{formattedTime}, LocaleContextHolder.getLocale());
        return ApiResponse.success(message);
    }

    /**
     * 测试路径参数验证
     */
    @GetMapping("/{id}")
    public ApiResponse<String> getUserById(@PathVariable @Min(value = 1, message = "{validation.min}") Long id) {
        String message = messageSource.getMessage("common.success", null, LocaleContextHolder.getLocale());
        return ApiResponse.success(message + " - User ID: " + id);
    }

    /**
     * 测试查询参数验证
     */
    @GetMapping("/search")
    public ApiResponse<String> searchUsers(
            @RequestParam @Size(min = 2, max = 20, message = "{validation.size}") String keyword,
            @RequestParam(defaultValue = "1") @Min(value = 1, message = "{validation.min}") Integer page) {
        String message = messageSource.getMessage("common.success", null, LocaleContextHolder.getLocale());
        return ApiResponse.success(message + " - Keyword: " + keyword + ", Page: " + page);
    }
} 